﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mads.CoreDomain;

namespace MADSite
{
  public partial class Tickets : System.Web.UI.Page
  {
    Station[] massiveOfStations;
    TrainManagerClient client = new TrainManagerClient();

    private int id = -1;
    TrainManagerClient trainManagerClient = new TrainManagerClient();
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!Request.Cookies.AllKeys.Contains("id"))
      {
        id = trainManagerClient.InitUser();
      }
      else
      {
        id = Int32.Parse(Request.Cookies["id"].Value);
      }
      HttpCookie cookie = new HttpCookie("id", id.ToString());
      Response.Cookies.Add(cookie);
      if (IsPostBack)
      { return; }

      massiveOfStations = client.GetStations(id);
      startDrop.Items.Clear();
      finishDrop.Items.Clear();
      foreach (Station station in massiveOfStations)
      {
        startDrop.Items.Add(station.Name);
        finishDrop.Items.Add(station.Name);
      }
    }

    /// <summary>
    /// Перейти к выбору рейса
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void nextButton_Click(object sender, EventArgs e)
    {
      DateTime date = dateCalendar.SelectedDate;
      string dateString = date.ToString();
      //string dateString = date.ToString("dd MM yyyy H mm\n");
      // Куки отправления
      HttpCookie startCookie = new HttpCookie("startPoint");
      startCookie.Value = startDrop.Text;
      startCookie.Expires = DateTime.Now.AddDays(1);
      Response.Cookies.Add(startCookie);
      // Куки прибытия
      HttpCookie finishCookie = new HttpCookie("finishPoint");
      finishCookie.Value = finishDrop.SelectedValue;
      finishCookie.Expires = DateTime.Now.AddDays(1);
      Response.Cookies.Add(finishCookie);
      // Куки даты
      HttpCookie dateCookie = new HttpCookie("date");
      dateCookie.Value = dateString;
      dateCookie.Expires = DateTime.Now.AddDays(1);
      Response.Cookies.Add(dateCookie);

      Response.Redirect("Tickets2.aspx");
    }
  }
}